package com.woniuxy.sensitive.service.impl;

import com.baidu.aip.contentcensor.AipContentCensor;
import com.woniuxy.sensitive.service.BaiduAuditService;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

@Service
public class BaiduAuditServiceImpl implements BaiduAuditService {
    private final AipContentCensor client;

    public BaiduAuditServiceImpl(AipContentCensor client) {
        this.client = client;
    }

    @Override
    public boolean auditText(String text) {
        JSONObject response = client.textCensorUserDefined(text);
        return response.optInt("conclusionType", 1) == 1;
    }

    @Override
    public String getRejectReason(String text) {
        JSONObject response = client.textCensorUserDefined(text);
        if (response.optInt("conclusionType", 1) != 1) {
            return response.optJSONArray("data")
                    .optJSONObject(0)
                    .optString("msg", "内容违规");
        }
        return "审核通过";
    }
}